package boot.provider;

import boot.beans.Message;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.RandomUtil;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <b><code>KafkaSender</code></b>
 *
 * <p>Description 发送
 *
 * <p><b>Creation Time:</b>2020-08-06-19:15
 *
 * @author hailan
 * @since kafkalearning 0.0.1
 */
@Component
// @Slf4j
public class KafkaSender {
  private static final Logger logger = LoggerFactory.getLogger(KafkaSender.class);
  @Resource private KafkaTemplate<String, String> kafkaTemplate;

  private Gson gson = new GsonBuilder().create();

  // 发送消息方法
  public void send() {
    Message message = new Message();
    Message.Point point = new Message.Point();
    List<Double> coordinates = new ArrayList<>();
    coordinates.add(23.81816991);
    coordinates.add(113.9631093);
    point.setCoordinates(coordinates);
    point.setType("point");
    message.setBizData(RandomUtil.randomString(20));
    message.setBizId(IdUtil.fastUUID());
    message.setCoordinateSystem("wgt84");
    message.setCreateTime(System.currentTimeMillis());
    message.setGeometryType("Point");
    message.setLayerCode("early_warning_alarm_events");
    message.setLayerName("高效处置一件事");
    message.setOperation("add");
    message.setResourceName(RandomUtil.randomString(20));
    message.setSourceApp("shb");
    message.setPoint(point);

    logger.info("发送的消息内容为: {}", gson.toJson(message));
    kafkaTemplate.send("topic-main", gson.toJson(message));
  }
}
